<?php
ob_start ();
define ( 'APP_DIR', dirname ( __FILE__ ) );
define ( 'SITE_WEB_DISK_DIR', dirname ( constant ( 'APP_DIR' ) ) );
include_once (constant ( 'APP_DIR' ) . '/config/config.enviroment.php');
include_once (constant ( 'APP_DIR' ) . '/config/config.string.php');
include_once (constant ( 'APP_DIR' ) . '/config/global.var.php');
include_once (constant ( 'PATH_INCLUDE' ) . '/class.db.php');
include_once (constant ( 'PATH_INCLUDE' ) . '/init_class_load.php');

define('APP_JS_LIB_DIR',constant('SITE_URL_PATH').'/app/lib');
class Application {
	public $error_message = '';
	function __construct() {
		$this->error_message = '';
		//inital site meta key words from database
		$this->reloadSiteKeywordFromDb();
	}
	public function refreshSiteKeywordToDb()
	{
		Util::setGlobalValue('site_meta_key_word_json',$this->json_encode($GLOBALS['META_KEY_WORDS']));
	}
	public function reloadSiteKeywordFromDb()
	{
		$GLOBALS['META_KEY_WORDS'] = $this->json_decode(Util::getGlobalValue('site_meta_key_word_json'));
		
		if ( is_object($GLOBALS['META_KEY_WORDS']) ) {
			$GLOBALS['META_KEY_WORDS'] = get_object_vars($GLOBALS['META_KEY_WORDS']);
		}
	}
	private function permissionCheck() {
		$file_path = APP_DIR . $this->relative_path . '/permission-check.php';
		if (file_exists ( $file_path )) {
			include ($file_path);
		}
	}
	private function action()
	{
		$last_slash_index =  strripos($_SERVER['PHP_SELF'], '/');
		$file_path = '';
		if ( $last_slash_index === FALSE ) {
			$file_path = APP_DIR . $this->relative_path .'/'.$_SERVER['PHP_SELF'];
		}else{
			$file_path = APP_DIR . $this->relative_path .'/'.substr($_SERVER['PHP_SELF'], $last_slash_index);
		}
		if (file_exists ( $file_path )) {
			include ($file_path);
		}
	}
	function run() {
		try{
			if ( strripos($_SERVER['PHP_SELF'], 'web-admin') !== FALSE) {
				$this->relative_path = '/web-admin';
			}else{
				$this->relative_path = '/';
			}
			$this->permissionCheck ();
			$this->action ();
		}catch(Exception $ex){
			$this->error_message = $ex->getMessage();
		}
	}
    function formatDate($date=false, $date_format = 'Y-m-d')
    {
  
    	$ts = time();
    	if ( gettype($date) == 'string' ) {
    		$ts = strtotime($date);
    	}
    	else if (  gettype($date) == 'int' ) {
    		$ts = $date;
    	}
    	return date($date_format, $ts);
    }
    function saveUploadedFile($tmp_file_name, $dir, $target_file_name = FALSE){
    	if ( !$target_file_name ) {
    		$target_file_name = $tmp_file_name;
    	}
    	$tmpFilePath = constant('SITE_WEB_DISK_DIR').'/img/tmp/'.$tmp_file_name;
    	$ext = Util::getFileExt($tmpFilePath);
    	$ext = strtolower($ext);
    	
    	$websitePath = '/img/'.$dir;
    	$uploadDir = constant('SITE_WEB_DISK_DIR').$websitePath;
    	if ( !is_dir($uploadDir) ) {
    		mkdir($uploadDir);
    	}
    	$uploaded = FALSE;
    	if($ext == "png" || $ext == "jpg" || $ext == "jpeg" || $ext == "gif" || $ext == "bmp")
    	{
    		$targetFilePath  = 	$uploadDir.$target_file_name;
    		copy($tmpFilePath,$targetFilePath);
    		unlink($tmpFilePath);
    		$uploaded = TRUE;
    	}
    	return $uploaded;
    }
    function removeFile($file_name, $dir)
    {
    	$file_name = trim($file_name);
    	if ( strlen($file_name) == 0 ) {
    		return false;
    	}
    	$websitePath = '/img/'.$dir;
    	$uploadDir = constant('SITE_WEB_DISK_DIR').$websitePath;
    	$path = $uploadDir.$file_name;
    	if ( file_exists($path)) {
    		unlink($path);
    	}
    }
    function json_encode($data){
    	$json = new Services_JSON();
    	$json_response = array('error_code'=>'0');
    	return $json->encode($data);
    	
    }
    function json_decode($str){
    	$json = new Services_JSON();
    	$json_response = array('error_code'=>'0');
    	return $json->decode($str);
    
    }
    function trim_html_content($content,$length)
    {
    	$content = html_entity_decode($content);
    	$content = strip_tags($content);
    	$need_more_dot = mb_strlen($content,'UTF-8') > $length;
    	$content = mb_substr($content,0,$length,'UTF-8');
    	if ( $need_more_dot ) {
    	$content .= '...';
    	}
    	return $content;
    }
    function get_article_detail_page_url($article_row)
    {
    	$page_name = '';
    	if ( $article_row['display_style_id'] == 1) {
//     		if ( $article_row['cat_id'] == 1 ) {
//     			$page_name = 'contactd.php';
//     		}else{
//     			$page_name = 'contacta.php';
//     		}
    		
    		$page_name = 'contacta.php';
    		
    	}
    	elseif ( $article_row['display_style_id'] == 2) {
    		$page_name = 'contactb.php';
    	}
    	elseif ( $article_row['display_style_id'] == 3) {
    		$page_name = 'contactc.php';
    	}
    	$page_name .='?id='.$article_row['id'];
    	if ( $article_row['display_style_id'] == 3) {
    		$page_name .= '" target="_blank"';
    	}
    	return $page_name;
    }
    
    function meta_idStr_to_valueStr($idStr)
    {
    	$id_list = explode(',', $idStr );
    	$val_list = array();
    	foreach($id_list as $id)
    	{
    		$val_list[] = $GLOBALS['META_KEY_WORDS'][$id];
    	}
    	return implode(',', $val_list );
    }
    function send_out_email($email,$subject,$body)
    {
    	include_once(constant('APP_DIR').'/lib/phpmailer/class.phpmailer.php');
    	$mail = new PHPMailer();
    	$mail->CharSet = 'UTF-8';
    	$mail->Encoding = "base64";	
    	$mail->SetFrom('noreplay@benetton.com', 'Benetton');
    	$address = $email;
    	$mail->AddAddress($address);
    	$mail->Subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";
    	
    	$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    	
    	$mail->MsgHTML($body);
    	
    	$mail->Send();
    }
    function isAccessFromPC()
    {
    	return Util::isAccessFromPC();
    }
}

$app = new Application ();
$GLOBALS['APP_INSTANCE'] = $app;
$app->run ();
